/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import common.Constant;
import dao.ImportExportBillDAO;
import dao.OrderDAO;
import dao.OrderDetailDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.BillDetailModel;
import model.ImportExportBillModel;
import model.OrderDetailsModel;
import model.OrderModel;

/**
 *
 * @author ThanhToan
 */
public class OrderController extends HttpServlet {
    private String URL = "";

    private void mapParameterToModel(HttpServletRequest req
			,HttpServletResponse res
				,OrderModel orderModel) {
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request, response);
    } 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("review") != null)
	{
            review(request, response);
	}
    }

    private void review(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        OrderModel orderModel = new OrderModel();
        String orderID = request.getParameter("orderID");
        orderModel.setId(orderID);
        if(request.getParameter("action1")!=null){
            if(request.getParameter("action1").toString().equals("approve")){
                orderModel.setStatus("1");
            }
            else{
                orderModel.setStatus("2");
            }
        }
        orderModel.setApprovedBy((String) request.getSession().getAttribute("userID"));
        if(request.getParameter("reason")!=null){
            orderModel.setRejectReason(request.getParameter("reason").toString());
        }

        if(OrderDAO.reviewOrder(orderModel)){
            URL = "//order.jsp?msg='update'";
            if(orderModel.getStatus().equals("1")){
                ArrayList<OrderDetailsModel> orderDetailList = OrderDetailDAO.getOrderDetailsByOrderID(orderID);
                orderModel = new OrderModel();
                orderModel = OrderDAO.getOrderByID(orderID);
                orderModel.setOrderDetailList(orderDetailList);
//                new import model
                ImportExportBillModel importModel = new ImportExportBillModel();
                importModel.setCusID(orderModel.getAccID());
                importModel.setTotalPrice(orderModel.getTotalAmount());
                importModel.setDiscount(orderModel.getDiscount());
                importModel.setOrderID(orderModel.getId());
//                new bill details
                ArrayList<BillDetailModel> billList = new ArrayList<BillDetailModel>();
                for(OrderDetailsModel obj : orderDetailList){
                    BillDetailModel billModel = new BillDetailModel();
                    billModel.setProductID(obj.getProductID());
                    billModel.setNumber(obj.getNumber());
                    billModel.setPrice(obj.getPrice());
                    billModel.setAmount(obj.getValue());
                    billList.add(billModel);
                }
                importModel.setBillDetail(billList);
                try {
//                    add exportbill
                    ImportExportBillDAO.insertExportBill(importModel);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        RequestDispatcher rd = request.getRequestDispatcher(URL);
        rd.forward(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
